﻿(function () {
    "use strict";
    var that;
    var personCollection;

    var vm = Vue.component('crud-list', {
        template: templates["list"],
        data : function(){
            return {
                persons : []
            }
        },
        methods: {
            init(collection) {
                personCollection = collection;
            },
            reload : function(query){
                that.persons = personCollection.get();                
            },
            fetch(query) {
                return personCollection.fetch(query).done(function (response) {
                    that.reload();
                });
            },
            triggerViewClick: function (person) {
                that.$emit("editClick", person);
            },            
            triggerDeleteClick: function (id) {
                personCollection.remove(id).done(function () {                    
                    that.$emit("deleteClick", id);
                    that.reload();
                });                
            }
        },
        mounted: function () {
            that = this;            
        }
    });
})();